package com.bokesoft.yigo.mid.service.example1;

import java.util.ArrayList;

import com.bokesoft.yigo.common.util.TypeConvertor;
import com.bokesoft.yigo.meta.dataobject.MetaDataObject;
import com.bokesoft.yigo.meta.factory.IMetaFactory;
import com.bokesoft.yigo.meta.form.MetaForm;
import com.bokesoft.yigo.mid.base.DefaultContext;
import com.bokesoft.yigo.mid.connection.IDBManager;
import com.bokesoft.yigo.mid.document.DeleteFormData;
import com.bokesoft.yigo.mid.document.LoadFormData;
import com.bokesoft.yigo.mid.service.IExtService;
import com.bokesoft.yigo.struct.document.Document;
import com.bokesoft.yigo.struct.env.Env;
import com.bokesoft.yigo.tools.ve.VE;
/**
 * 对应Example4DBOpt表单，点击按钮，通过OID删除数据
 * @author ljd
 *
 */
public class DeleteExample4Service implements IExtService {

	@Override
	public Object doCmd(DefaultContext context, ArrayList<Object> paras) throws Throwable {
		String formKey = TypeConvertor.toString(paras.get(0));
		long oid = TypeConvertor.toLong(paras.get(1));
		LoadFormData load = new LoadFormData(formKey, oid);
		Document document = load.load(context, null);
		DeleteFormData delete = new DeleteFormData(formKey, document);
		delete.delete(context);
		return true;
	}

	private void demoContext(DefaultContext context) throws Throwable {
		VE ve = context.getVE();
		IMetaFactory metaFactory = ve.getMetaFactory();
		
		// 取得表单对象
		MetaForm metaForm = metaFactory.getMetaForm("MyForm");
		
		// 取得数据对象
		MetaDataObject metaDataObject = metaFactory.getDataObject("MyDataObject");
		
		
		String formKey = "";
		MetaForm metaForm1 = metaFactory.getMetaForm(formKey);
		
		Env env = ve.getEnv();
		// 取得会话ID
		String clietnID = env.getClientID();
		
		// 取得当前用户
		long userID = env.getUserID();
		// 取得当前用户代码
		String userCode = env.getUserCode();
		// 取得当前用户名称
		String userName = env.getUserName();
	}
	
	private void demo2Context(DefaultContext context) throws Throwable {
		VE ve = context.getVE();
		IMetaFactory metaFactory = ve.getMetaFactory();		
		
		String formKey = "";
		MetaDataObject metaDataObject = null;
		MetaForm metaForm = metaFactory.getMetaForm(formKey);
		String dataObjectKey = metaForm.getDataSource().getRefObjectKey();
		if ( dataObjectKey != null && !dataObjectKey.isEmpty() ) {
			// 外联数据对象
			metaDataObject = metaFactory.getDataObject(dataObjectKey);
		} else {
			// 内联数据对象
			metaDataObject = metaForm.getDataSource().getDataObject();
		}
		
		// 取得文档对象
		Document document = context.getDocument();
		
		// 取得数据库连接
		IDBManager db = context.getDBManager();
	}
}
